﻿using ChloeEx.Core;
using Microsoft.Extensions.Configuration;
using System;
using Chloe;
using Chloe.MySql;
using MySql.Data.MySqlClient;
using ChloeEx.Core.MySql;
using System.Data;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
            .AddJsonFile("appsetting.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables(); 

            Configuration = builder.Build();
            var connectionstring=Configuration.GetConnectionString("DefaultConnection");
            Console.WriteLine(connectionstring);
            Setting.Instance = new Setting(connectionstring,CreateDbContext);
            Setting.Instance.PrintSQL(new ConsolePrinter());
            again:
            var manager = EntityManagerFactory.CreateDbContext();
            Console.WriteLine(manager.Insert(new usersEntity() { UserJobID = "111", Email = "sdfsdfds1", UserName = "aaaaa1" }).Id);
            Console.WriteLine(manager.Insert(new usersEntity() { UserJobID = "222", Email = "sdfsdfds2", UserName = "aaaaa2" }).Id);
            Console.WriteLine(manager.Insert(new usersEntity() { UserJobID = "333", Email = "sdfsdfds3", UserName = "aaaaa3" }).Id);
            Console.WriteLine(manager.Insert(new usersEntity() { UserJobID = "444", Email = "sdfsdfds4", UserName = "aaaaa4" }).Id);
            Console.ReadLine();
            goto again;
        }
        static private DbContext CreateDbContext(string connectionstring)
        {
            var connectionfactory = new ConnectionFactory(CreateConnection);
            return new MySqlContext(connectionfactory);
        }
        static private IDbConnection CreateConnection(string connectionstring)
        {
            return new MySqlConnection(connectionstring);
        }
        public static IConfiguration Configuration { get; set; }
    }
}
